package com.blast.cn.ui

import android.app.Dialog
import android.content.Context
import android.text.InputType
import android.text.TextUtils
import android.view.KeyEvent
import com.blast.cn.R
import com.blast.cn.http.MainHttps
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.http.SerialHttpCallback
import com.wxl.common.util.KeyBoardUtil
import com.wxl.common.util.StringUtils
import com.wxl.common.util.Toll
import com.wxl.common.wiget.LoadingDialog
import kotlinx.android.synthetic.main.activity_updatepwd_layout.*

class UpdatePwdActivity : AbsActivity(){

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_updatepwd_layout
    }

    override fun onCreateViewChanged() {
        updatePwdOldImg.setOnClickListener {
            updatePwdOldImg.isEnabled = !updatePwdOldImg.isEnabled
            updatePwdOldImg.setBackgroundResource(if(updatePwdOldImg.isEnabled) R.mipmap.icon_psd_show else R.mipmap.icon_psd_hide)
            updatePwdOldEt.inputType = if(updatePwdOldImg.isEnabled) InputType.TYPE_CLASS_TEXT else InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
        updatePwdNew0Img.setOnClickListener {
            updatePwdNew0Img.isEnabled = !updatePwdOldImg.isEnabled
            updatePwdNew0Img.setBackgroundResource(if(updatePwdNew0Img.isEnabled) R.mipmap.icon_psd_show else R.mipmap.icon_psd_hide)
            updatePwdNew0Et.inputType = if(updatePwdOldImg.isEnabled) InputType.TYPE_CLASS_TEXT else InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
        updatePwdNew1Img.setOnClickListener {
            updatePwdNew1Img.isEnabled = !updatePwdOldImg.isEnabled
            updatePwdNew1Img.setBackgroundResource(if(updatePwdNew1Img.isEnabled) R.mipmap.icon_psd_show else R.mipmap.icon_psd_hide)
            updatePwdNew1Et.inputType = if(updatePwdOldImg.isEnabled) InputType.TYPE_CLASS_TEXT else InputType.TYPE_TEXT_VARIATION_PASSWORD
        }

        updatePwdCancel.setOnClickListener {
            finish()
        }
        updatePwdSure.setOnClickListener {
            KeyBoardUtil.hideSoftInput(updatePwdOldEt)
            KeyBoardUtil.hideSoftInput(updatePwdNew0Et)
            KeyBoardUtil.hideSoftInput(updatePwdNew1Et)
            submit()
        }
    }


    private fun submit(){
        if(TextUtils.isEmpty(updatePwdOldEt.text)){
            Toll.toll("请输入原密码")
            return
        }

        val loginAcc = AppContext.appContext.getLoginAcc()
        if(loginAcc != null){
            val p = StringUtils.string2MD5(updatePwdOldEt.text.toString())
            if(!TextUtils.equals(p,loginAcc.userpass)){
                Toll.toll("原密码不正确")
                return
            }
        }

        if(TextUtils.isEmpty(updatePwdNew0Et.text)){
            Toll.toll("请输入新密码")
            return
        }
        if(TextUtils.isEmpty(updatePwdNew1Et.text)){
            Toll.toll("请确认新密码")
            return
        }
        if(TextUtils.equals(updatePwdNew1Et.text,updatePwdNew0Et.text)){
            Toll.toll("新密码输入不一致")
            return
        }

        MainHttps.updatePwd(updatePwdOldEt.text.toString(),object : SerialHttpCallback<String>(""){
            override fun loadDataSuccess(data: String?) {
                Toll.toll("修改成功")
                finish()
            }

            override fun loadDataFailed(e: Throwable) {
                Toll.toll("修改失败")
            }

            override fun showLoadDialog(context: Context?): Dialog? {
                return LoadingDialog(context!!)
            }
        })

    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        KeyBoardUtil.hideSoftInput(updatePwdOldEt)
        KeyBoardUtil.hideSoftInput(updatePwdNew0Et)
        KeyBoardUtil.hideSoftInput(updatePwdNew1Et)
    }

    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_BACK){
            updatePwdSure.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_ENTER){
            updatePwdSure.performClick()
        }
        return true
    }
}